package com.example.myapplication.network;

import com.commentlib.R;
import com.commentlib.utils.SPUtils;
import com.commentlib.utils.StringUtils;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.ovo.network.utils.IdeaApi.getServerApi;

/**
 * 服务器地址配置
 */
public class RetrofitHelper {
    private static ApiService apiService;

    //正式
//    public static String wsurl = "ws://47.112.207.240:9999/websocket/";
//    public static String wsurl = "wss://wlc.renliguanli.com/websocket/";
//    public static String SERVER = "https://wlc.renliguanli.com/";
//    public static String SERVERS = "https://wlc.renliguanli.com";
    //测试
    public static String wsurl = "ws://www.wlchrs.com:8088/websocket/";
    public static String SERVER = "http://www.wlchrs.com:8088/";
    public static String SERVERS = "http://www.wlchrs.com:8088";

    static {
//        if (BuildConfig.platform == 8080 || BuildConfig.platform == 8088){
//            if ("8080".equals(SPUtils.get(SPUtils.platform, "8088"))){
//                wsurl = "ws://192.168.1.114:8080/websocket/";
//                SERVER = "http://192.168.1.114:8080/";
//                SERVERS = "http://192.168.1.114:8080";
//            }else if ("8088".equals(SPUtils.get(SPUtils.platform, "8088"))){
//                wsurl = "ws://www.wlchrs.com:8088/websocket/";
//                SERVER = "http://www.wlchrs.com:8088/";
//                SERVERS = "http://www.wlchrs.com:8088";
//            }else if ("80".equals(SPUtils.get(SPUtils.platform, "8088"))){
//                wsurl = "wss://wlc.renliguanli.com/websocket/";
//                SERVER = "https://wlc.renliguanli.com/";
//                SERVERS = "https://wlc.renliguanli.com";
//            }
//        }
        apiService = getServerApi(ApiService.class, SERVER);
    }

    public static ApiService getApiServer() {
        return apiService;
    }

    /**
     * session会话过期重新登录
     * @param toBeResumed
     * @param <T>
     * @return
     */
    public static <T> Function<Throwable, ? extends Observable<? extends T>>
    refreshTokenAndRetry(final Observable<T> toBeResumed, HashMap<String, Object> reqMap) {
        return new Function<Throwable, Observable<? extends T>>() {
            @Override
            public Observable<? extends T> apply(Throwable t) {
                if ("403".equals(t.getMessage())){
                    String account = (String) SPUtils.get(SPUtils.account, "");
                    String pwd = (String) SPUtils.get(SPUtils.pwd, "");

                    Observable observable = null;
                    if (!StringUtils.isEmpty(account) && !StringUtils.isEmpty(pwd)){
                        HashMap<String, Object> map = new HashMap<>();
//                        observable = RetrofitHelper.getApiServer().loginPwd(map);
                    }else {
//                        IntentUtils.GoActivity(LoginActivity.class);
                        return Observable.error(t);
                    }
                    if (observable == null){
//                        IntentUtils.GoActivity(LoginActivity.class);
                        return Observable.error(t);
                    }
                    return (Observable<? extends T>) observable.subscribeOn(Schedulers.newThread())
                            .observeOn(Schedulers.newThread())
                            .flatMap(new Function<Object, ObservableSource<? extends R>>() {
                                @Override
                                public ObservableSource<? extends R> apply(Object response) {
//                                    BaseApplication.intance.updateLoginInfo(response);
//                                    reqMap.put("__sid", response.getSid());
                                    return (ObservableSource<? extends R>) toBeResumed;
                                }
                            });
                }
                return Observable.error(t);
            }
        };
    }
}
